package event;

import giaodien.DangNhap;
import giaodien.FormChinh;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import main.Main;

public class MyButtonEvent implements ActionListener{
	FormChinh form;
	Main main;
	boolean mok=false;
	DangNhap dangnhap=new DangNhap();
	public MyButtonEvent(FormChinh form) {
		this.form=form;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource()==form.menubar.btndangnhap){
			form.dangnhap.setVisible(true);
			form.setEnabled(false);
			form.dangnhap.setAlwaysOnTop(true);
		}
		if(arg0.getSource()==form.dangnhap.option){
			if(mok==false){
				form.dangnhap.setSize(440, 360);
				mok=true;
				return;
			}else{
				form.dangnhap.setSize(440, 244);
				mok=false;
			}
		}
		if(arg0.getSource()==form.dangnhap.huy){
			System.exit(0);
		}
		if(arg0.getSource()==form.dangnhap.dangnhap){
			form.dangnhap.setVisible(false);
			form.setEnabled(true);
			form.setAlwaysOnTop(true);
		}
		if(arg0.getSource()==form.hanghoa.tolbar.newbtn){
			form.hanghoa.newhanghoa.setVisible(true);
		}
		if(arg0.getSource()==form.hanghoa.newhanghoa.dongy){
			form.hanghoa.ketnoi.excuteQuery("insert into hanghoa values('"+form.hanghoa.newhanghoa.txtmah.getText().trim()+"','"+form.hanghoa.newhanghoa.txttenh.getText().trim()+"','"+form.hanghoa.newhanghoa.txtman.getText().trim()+"','"+form.hanghoa.newhanghoa.txtdv.getText().trim()+"','"+form.hanghoa.newhanghoa.txtdb.getText().trim()+"')",false);
			form.hanghoa.newhanghoa.setVisible(false);
			//((DefaultTableModel)form.hanghoa.table.getModel()).insertRow(form.hanghoa.table.getRowCount()+1, new Object[]{"+form.hanghoa.newhanghoa.txtmah.getText().trim()+","+form.hanghoa.newhanghoa.txttenh.getText().trim()+","+form.hanghoa.newhanghoa.txtman.getText().trim()+","+form.hanghoa.newhanghoa.txtdv.getText().trim()+","+form.hanghoa.newhanghoa.txtdb.getText().trim()+"});
		}
		if(arg0.getSource()==form.hanghoa.newhanghoa.huy){
			form.hanghoa.newhanghoa.setVisible(false);
		}
		if(arg0.getSource()==form.hanghoa.suahanghoa.huy){
			form.hanghoa.newhanghoa.setVisible(false);
		}
		if(arg0.getSource()==form.hanghoa.suahanghoa.dongy){
			 int[] row=form.hanghoa.table.getSelectedRows();
			 String mah = null;
			 mah=(String)form.hanghoa.table.getValueAt(row[0],0);
			form.hanghoa.ketnoi.excuteQuery("update hanghoa set tenh='"+form.hanghoa.suahanghoa.txttenh.getText().trim()+"',man='"+form.hanghoa.suahanghoa.txtman.getText().trim()+"',dvt='"+form.hanghoa.suahanghoa.txtdv.getText().trim()+"',db='"+form.hanghoa.suahanghoa.txtdb.getText().trim()+"' where mah='"+mah+"'",false);
			form.hanghoa.suahanghoa.setVisible(false);
		}
		if(arg0.getSource()==form.hanghoa.tolbar.openbtn){
			form.hanghoa.suahanghoa.setVisible(true);
			 int[] row=form.hanghoa.table.getSelectedRows();
			 String mah = null;
			 String tenh = null;
			 String man = null;
			 String dv = null;
			 String db = null;
			 mah=(String)form.hanghoa.table.getValueAt(row[0],0);
			 tenh=(String)form.hanghoa.table.getValueAt(row[0],1);
			 man=(String)form.hanghoa.table.getValueAt(row[0],2);
			 dv=(String)form.hanghoa.table.getValueAt(row[0],3);
			 db=(String)form.hanghoa.table.getValueAt(row[0],4);
			 form.hanghoa.suahanghoa.txtdb.setText(db);
			 form.hanghoa.suahanghoa.txtdv.setText(dv);
			 form.hanghoa.suahanghoa.txtmah.setText(mah);
			 form.hanghoa.suahanghoa.txttenh.setText(tenh);
			 form.hanghoa.suahanghoa.txtman.setText(man);
		}
		if(arg0.getSource()==form.hanghoa.tolbar.closebtn){
			 String selectedData = null;
			 int[] row=form.hanghoa.table.getSelectedRows();
			 selectedData=(String)form.hanghoa.table.getValueAt(row[0],0);
			 int n=JOptionPane.showConfirmDialog(form.hanghoa, "Bạn có muốn xóa mã hàng:"+selectedData+" không", "Cảnh Báo", JOptionPane.YES_NO_OPTION);
				 if(n==JOptionPane.YES_OPTION){
					//System.exit(0);
					form.hanghoa.ketnoi.excuteQuery("delete from hanghoa where mah='"+selectedData+"'",false);
				 }
				 if(n==JOptionPane.NO_OPTION){
					form.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
				 }
		}
	}
}
